package com.doc.test;

import com.doc.bean.URLSAVE;
import com.google.gson.Gson;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Administrator
 *
 */
public class test {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static String getresult(String path){
		String result = "";
		BufferedReader br = null;
		try {
			URL url = new URL(path);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			
			conn.setInstanceFollowRedirects(true);
			conn.setDoOutput(true);
            
            // 设置连接输入流为true
            conn.setDoInput(true);
            
            // 设置请求方式为post
            conn.setRequestMethod("POST");
            
            // post请求缓存设为false
            conn.setUseCaches(false);
            conn.connect();
			br= new BufferedReader(new InputStreamReader(
					conn.getInputStream()));
			String line;
			while ((line = br.readLine()) != null) {
				result = line;
			}

		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if(br!=null){
				br.close();
			}
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
		return result;

	}
	public static void getEnglish() throws IOException{
		Document doc = Jsoup.connect("http://news.iciba.com/dailysentence/history.html").get();
        Elements es = doc.select("div.clear");
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        for (int ia =0;ia<8;ia++){
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("zh",es.select("div.main.fl div.sort_list div.cont_list_mod div.c_l_m_rg.fr p.c_l_m_cn").get(ia).text());
            map.put("en",es.select("div.main.fl div.sort_list div.cont_list_mod div.c_l_m_rg.fr p.c_l_m_en").get(ia).text());
            list.add(map);
        }
		System.out.println(new Gson().toJson(list));
	}
	public static String getSomeOkText(String patternStrZhengze,String targetStr){
		Pattern pattern = Pattern.compile(patternStrZhengze);
		Matcher matcher = pattern.matcher(targetStr);
		if (matcher.find()) {
			return matcher.group();
		}
		return "";
	}
	public void getHtmlText(String path){
		Document doc;
		try {
			doc = Jsoup.connect(path)
					.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0")
					.timeout(12000)
					.get();
			String es = doc.html();
			String a = HtmlRegexpUtil.zhenzepipei(es, HtmlRegexpUtil.regEx_style);
			String c = HtmlRegexpUtil.zhenzepipei(a, HtmlRegexpUtil.regEx_script);
			String d = HtmlRegexpUtil.zhenzepipei(c, HtmlRegexpUtil.regEx_special);
			String b = HtmlRegexpUtil.filterHtmlhua(HtmlRegexpUtil.replaceBlank(HtmlRegexpUtil.filterHtml(d)));
			System.out.println(b);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * @param zz 正则表达式
	 * @param str 需要匹配的字符串
	 * @return 返回Matcher对象
	 */
	public static Matcher zhenze(String zz,String str){
		Pattern zza = Pattern.compile(zz);
		Matcher m = zza.matcher(str);
		return m;
	}
	
	/**
	 * @param prehtml 传入html标签
	 * @return <a>的href和value的集合
	 */
	public static List[] getHtmlAName(String prehtml){
		List[] arr = new List[2];
		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		List<URLSAVE> list2 = new ArrayList<URLSAVE>();
		Map<String,String> map = new HashMap();
		Matcher x = zhenze("<a[^>]*>([^<]*)</a>",prehtml);
		int i = 1;
		while(x.find()){
			map = new HashMap();
			Matcher m = zhenze("href=\"([^\"]+)\"",x.group());
			m.find();
			map.put("名字"+i,x.group(1));
			map.put("网址"+i,m.group(1));
			URLSAVE u = new URLSAVE(x.group(1),null,m.group(1));
			i++;
			list.add(map);
			list2.add(u);
		}
		arr[0] = list;
		arr[1] = list2;
		return arr;
	}
	
	public static String geturr(String context){
		Pattern p = Pattern.compile("[^h]*?\\.(com|cn|net|org|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE); 
		Matcher m = p.matcher(context);
		m.find();
		return "h"+m.group();
	}
	
	public static void main(String[] args){
		
		String a = "dsadsa <a href=\"http://www.baidu.com\">百度</a>dsadsa<a href=\"http://www.baidu.com\">百度</a>";
		getHtmlAName(a);
		Matcher x = zhenze("<a[^>]*>([^<]*)</a>",a);
		x.find();
		System.out.println(x.group());
		Matcher m = zhenze("([^=]*)\"([^\"]*)\"",a);
		m.find();
		System.out.println("m:"+m.group(2));
		Matcher m1 = zhenze("<([^<]*)>",a);
		m1.find();
		System.out.println("m1:"+m1.group());
		Matcher m2 = zhenze("([^<]*)",a.replaceAll(m1.group(), ""));
		m2.find();
		System.out.println("m2:"+x.group(1));
        
	}

}
